package com.jx.lcs.messagepush.dao;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.jx.lcs.messagepush.entity.UserMessage;
import com.tianyu.jty.common.persistence.HibernateDao;

@Repository
public class UserMessageDao  extends HibernateDao<UserMessage, String>{

	private static final Logger logger = LoggerFactory.getLogger(UserMessageDao.class);
	
	public List getUnReadMsgCount(){
		List list = null;
		try{
			StringBuffer sb = new StringBuffer();
			sb.append("select uid, count(*) num from tb_user_message where readstatus='0' group by uid");
			SQLQuery sqlQuery = createSQLQuery(sb.toString());
			return sqlQuery.list();
		} catch(Exception e) {
			logger.error("--获取未读信息条数异常！", e);
			return list;
		}
	}
	
	public void batchSave(HashSet<UserMessage>  userMessageSet){
		int i=0;
		Session session = this.sessionFactory.openSession();
		try {
			for(Iterator<UserMessage> iterator = userMessageSet.iterator();iterator.hasNext();){
				session.saveOrUpdate(iterator.next());
				if ((i++) % 50 == 0){
					session.flush();
					session.clear();
				}
			}
		} catch (Exception e) {
			logger.info("推送消息到所有用户出现异常", e);
		} finally {
			session.flush();
			session.close();
		}
	}
}
